import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:visitor_scan_package/res/color_res.dart';
import 'package:visitor_scan_package/res/demins.dart';
import 'package:visitor_scan_package/viewmodel/login_view_model.dart';

class LoginPage extends BaseStatefulMvvmWidget {
  final String? loginMsg;

  LoginPage({Key? key, this.loginMsg}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _LoginPageState();
}

class _LoginPageState extends BaseMvvmState<LoginViewModel, LoginPage> {
  @override
  Widget? buildLoadingContentView() {
    return Column(
      children: [
        Container(
          height: 40,
        ),
        Image.asset(
          "images/new_logo.png",
          width: 120,
          height: 80,
        ),
        Container(height: 16),
        CommonText('智慧养老院\n访客核销',
            textSize: 22, textColor: const Color(0xff313233), fontWeight: FontWeight.bold,textAlign: TextAlign.center),
        Container(height: 40,),
        Column(
          children: [
            CommonInput(
              headWidget: Image.asset("images/username.png").addToContainer(margin: EdgeInsets.only(right: 10)),

              text: viewModel.username,
              hintText: "请输入用户名",
              textSize: Demins.textSize,
              onChanged: (str) {
                viewModel.username = str;
              },
              // headWidget: Image.asset(
              //   Drawables.loginUser,
              //   width: 15,
              // ).addToContainer(margin: EdgeInsets.only(right: 5)),
            ),
            CommonInput(
              text: viewModel.pdd,
              headWidget: Image.asset("images/password.png").addToContainer(margin: EdgeInsets.only(right: 10)),

              hintText: "请输入密码",
              needClear: true,
              showPassword: true,
              maxLines: 1,
              textSize: Demins.textSize,
              keyboardType: CommonInputType.password,
              onChanged: (str) {
                viewModel.pdd = str;
              },
              // headWidget: Image.asset(
              //   Drawables.loginPassword,
              //   width: 15,
              // ).addToContainer(margin: EdgeInsets.only(right: 5)),
            ),
            Container(
              height: 20,
            ),
            ElevatedButton(
                onPressed: onClick(
                  () {
                    viewModel.login();
                  },
                ),
                style: ButtonStyle(
                    backgroundColor:
                        MaterialStateProperty.all(ColorRes.main_color),
                    minimumSize: MaterialStateProperty.all(
                        const Size(double.infinity, 49))),
                child:
                    CommonText("登录", textColor: Colors.white).addToContainer())
          ],
        ).addToContainer(margin: const EdgeInsets.symmetric(horizontal: 20)),
        Container().setWeight(1),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset("images/yyt_logo.png",),
            CommonText('颐养通技术支持',textSize: 14,textColor: Color(0xff939699))
          ],
        ).addToContainer(margin: const EdgeInsets.only(bottom: 21))
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("访客核销"),
      ),
      body: loadingContentWidget,
      resizeToAvoidBottomInset: false,
    );
  }

  @override
  LoginViewModel createViewModel() {
    return LoginViewModel(context);
  }

  @override
  void onRetryClick() {

  }

  @override
  void onCreate() {
    super.onCreate();

  }

  @override
  void onViewModelCreated() {
    super.onViewModelCreated();
    if(!widget.loginMsg.isNullOrEmpty()){
      showCupertinoDialog(context: context, builder: (context){
        return CupertinoAlertDialog(content: CommonText('登录信息已超时，请重新登录'),
          actions: [TextButton(onPressed: (){
            finish();
          }, child: CommonText('确定',textColor: ColorRes.main_color))],);
      });
    }
  }
}
